/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package reminder;

import abstractmodel.*;
import dataaccess.IDataSource;
import java.util.Calendar;
import javax.swing.*;
import java.text.*;

/**
 *
 * @author littlechild
 */
public class RepayReminder implements ReminderObject {
    public static final int WINDOW_REMIND=0;
    public static final int EMAIL_REMIND=1;
    int day;
    int d; //提前d天提醒
    String username;
    int account_id;
    int mode;

    String remindingTile="smallpie.iWallet repay reminder";
    String remindingBody;
    public RepayReminder(String username,int account_id) {
        this.username=username;
        this.account_id=account_id;
    }
    public void setRepayDay(int day) {
        this.day=day;
    }
    public void setReminderMode(int mode) {
        this.mode=mode;
    }
    public void setAheadTime(int d) {
        this.d=d;
    }
    public boolean isSatisfied(IDataSource data) {
        //check the date
        Calendar currentCalendar=Calendar.getInstance();
        currentCalendar.setTimeInMillis(System.currentTimeMillis());
        
        Calendar repayCalendar=(Calendar)currentCalendar.clone();
        repayCalendar.set(Calendar.DAY_OF_MONTH, day);
        if(repayCalendar.before(currentCalendar)) { 
            repayCalendar.add(Calendar.MONTH,1); //next month
        }
        
        Calendar remindCalendar=(Calendar)repayCalendar.clone();
        remindCalendar.add(Calendar.DAY_OF_MONTH, -d);
        
        if(currentCalendar.before(remindCalendar))
            return false;
        //should remind
        IUser user=data.loadUser(username);
        IAccount account=Utilities.searchForAccount(user, account_id);
        if(account!=null) {
            double remainingMoney=account.getTotalLeftMoney();
            //check whether the amount of remaining money is negtive
            if(remainingMoney<0) {
                remindingBody="your account："+account.getAccountInfo().getValue("帐户名称").getString()
                    +"should repay"+DateFormat.getDateInstance().format(repayCalendar.getTime())
                    +"before"+NumberFormat.getCurrencyInstance().format(remainingMoney)
                    +"\r\n";
                return true;
            }
        }
        return false;
    }
    public void conduct() {
        // send the remind according to mode
          if(mode==WINDOW_REMIND) {
                JFrame w=new JFrame(remindingTile);
                w.add(new JLabel(remindingBody));
                w.setSize(400,300);
                w.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                w.setLocationRelativeTo(null);
                w.setVisible(true);
          }else if(mode==EMAIL_REMIND) {
            if(!Utilities.sendEmail(username, remindingTile, remindingBody)) {
                JFrame w=new JFrame(remindingTile);
                w.setSize(400,300);
                w.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                w.add(new JLabel(remindingBody+"PS:您的email提醒由于网络原因不能正常发送,请检查网络连接\r\n"));
                w.setLocationRelativeTo(null);
                w.setVisible(true);
            }
         }
    }
}
